package com.xxxx.yebserver.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelStyleType;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import com.xxxx.yebserver.common.R;

import com.xxxx.yebserver.common.validate.InsertGroup;
import com.xxxx.yebserver.common.validate.UpdateGroup;
import com.xxxx.yebserver.entity.Position;
import com.xxxx.yebserver.service.PositionService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxq
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/system/config/pos")
public class PositionController {
    @Autowired
    PositionService positionService;

    @ApiOperation("获取所有职位信息")
    @GetMapping("/")
    public List<Position> getAllPositions(){
        return positionService.list();
    }

    @ApiOperation("添加职位信息")
    @PostMapping("/")
    public R addPosition(@RequestBody @Validated({InsertGroup.class}) Position position){
        boolean save = positionService.save(position);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("更新职位信息")
    @PutMapping("/")
    public R updatePosition(@RequestBody @Validated({UpdateGroup.class}) Position position){
        boolean save = positionService.updateById(position);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("删除职位信息")
    @DeleteMapping("/{id}")
    public R deletePosition(@PathVariable("id") Long id){
        boolean save = positionService.removeById(id);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("批量删除职位信息")
    @DeleteMapping("/")
    public R deleteSelectPosition(@RequestBody List<Long> ids){
        boolean save = positionService.removeByIds(ids);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("excel导出")
    @GetMapping(value = "/exportExcel", produces = "application/octet-stream")
    public void exportExcel(HttpServletResponse resp){
        List<Position> list = positionService.list();
        ExportParams params = new ExportParams("职位表","职位表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Position.class, list);

        ServletOutputStream out = null;
        try {
            resp.setHeader("Content-Type","application/octet-stream");
            resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode("员工表.xls","UTF-8"));
            out = resp.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

